package app.server.project.main.service.service.upay.ton;

import app.server.project.core.constants.Def;
import app.server.project.main.service.db.dao.cfg.GlobalCfgDao;
import app.server.project.main.service.db.dao.user.UserWalletDao;
import app.server.project.main.service.service.upay.tron.BlockService;
import app.server.project.main.service.service.upay.tron.CfgService;
import app.server.project.main.service.service.upay.tron.EventService;
import cn.hutool.core.util.StrUtil;
import com.iwebpp.crypto.TweetNaclFast;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.ton.ton4j.address.Address;
import org.ton.ton4j.smartcontract.wallet.v1.WalletV1R3;
import org.ton.ton4j.utils.Utils;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import static app.server.project.main.service.service.upay.UPayType.Tron;

@RequiredArgsConstructor
@Service
@Slf4j
public class TonAccountService {

    private final static int Try_Time = 5 * 60;

    private final UserWalletDao userWalletDao;
    private final CfgService cfgService;
    private final GlobalCfgDao globalCfgDao;
    private final TaskScheduler taskScheduler;
    private final EventService eventService;
    private final BlockService blockService;

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
        TweetNaclFast.Signature.KeyPair keyPair = Utils.generateSignatureKeyPair();

        byte[] pubKey = keyPair.getPublicKey();
        byte[] secKey = keyPair.getSecretKey(); // 64 bytes


        log.info("pubKey: {}", Utils.bytesToHex(pubKey));
        log.info("pubKeyBase64: {}", Address.of("-1:544a753a2545750bdc83955e904e025a317eabd570dbd4c93bdeeafa890ce118").toString(true));
        log.info("secKey: {}", Utils.bytesToHex(secKey));

        WalletV1R3 contract = WalletV1R3.builder().build();

        log.info("Private key {}", Utils.bytesToHex(contract.getKeyPair().getSecretKey()));
        log.info("Public key {}", Utils.bytesToHex(contract.getKeyPair().getPublicKey()));
        log.info(
                "Non-bounceable address (for init): {}",
                contract.getAddress().toString(true, true, false, false));
        log.info(
                "Bounceable address (for later access): {}",
                contract.getAddress().toString(true, true, true, false));
        log.info("Raw address: {}", contract.getAddress().toString(true));

    }

    @EventListener(ApplicationReadyEvent.class)
    @Async(Def.CommonPool)
    public void init() {

    }

    /**
     * 获取tron用户钱包地址
     *
     * @param userId
     * @return
     */
    public String getTronAddress(long userId) {
        var data = userWalletDao.getWalletBy(userId, Tron);
        if (StrUtil.isBlank(data.getAddressHuman())) {
//            KeyPair keyPair = KeyPair.generate();
//            data.setAddressHuman(keyPair.toBase58CheckAddress());
//            data.setAddressHex("0x" + keyPair.toHexAddress().substring(2));
//            data.setPrivateKey(keyPair.toPrivateKey());
//            data.setPublicKey(keyPair.toPublicKey());
//            data.setBlockType(Tron);
            //更新钱包缓存
            userWalletDao.updateWallet(data);
            //this.blockService.addAddress(data.getAddressHuman());
        }
        return data.getAddressHuman();
    }


}
